package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.group.eams.pojo.dto.ItemCatalogQueryDto;
import com.group.eams.pojo.entity.ItemCatalog;
import com.group.eams.pojo.vo.ItemCatalogListVo;
import com.group.eams.service.ItemCatalogService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;



/**
 * 物品档案(ItemCatalog)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/itemCatalog")
public class ItemCatalogController  {
    /**
     * 服务对象
     */
    @Autowired
    private ItemCatalogService itemCatalogService;


    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list( ItemCatalogQueryDto itemCatalogQueryDto) {
        Page<ItemCatalogListVo> page = itemCatalogService.findByQueryDto(itemCatalogQueryDto);
        return Result.buildSuccess(page.getTotal(), page.getRecords());
    }

    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id){
        ItemCatalog itemCatalog = itemCatalogService.getById(id);
        return Result.buildSuccess(itemCatalog);
    }

    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll() {
        return Result.buildSuccess(itemCatalogService.list());
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody ItemCatalog itemCatalog) {
        boolean result = itemCatalogService.save(itemCatalog);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody ItemCatalog itemCatalog) {
        LambdaUpdateWrapper<ItemCatalog> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(ItemCatalog::getId, itemCatalog.getId());
        boolean  result = itemCatalogService.update(itemCatalog, queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id){
        LambdaQueryWrapper<ItemCatalog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ItemCatalog::getId, id);
        boolean result = itemCatalogService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){

        LambdaQueryWrapper<ItemCatalog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ItemCatalog::getId, batchDeleteDto.getIds());
        boolean result = itemCatalogService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }




}

